/**
 * 
 */
package com.qq.spirit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.qq.spirit.inter.AbstractActionAfterHttpRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

/**
 * 纺织任务
 * 
 * @author beedoorwei
 * 
 */
public class MakerTask extends TaskUnit
{
	static List<Integer> bmakeList = new ArrayList<Integer>();
	static Map<String, String> makerMap = new HashMap<String, String>();
	static
	{
		bmakeList.add(70);
		bmakeList.add(66);
		bmakeList.add(62);
		bmakeList.add(58);
		bmakeList.add(54);
		bmakeList.add(50);

		makerMap.put("70", "100");
		makerMap.put("69", "80");
		makerMap.put("68", "60");
		makerMap.put("67", "30");
		makerMap.put("66", "99");
		makerMap.put("65", "79");
		makerMap.put("64", "59");
		makerMap.put("63", "29");
		makerMap.put("62", "98");
		makerMap.put("61", "78");
		makerMap.put("60", "58");
		makerMap.put("59", "28");
		makerMap.put("58", "97");
		makerMap.put("57", "77");
		makerMap.put("56", "57");
		makerMap.put("55", "27");
		makerMap.put("54", "96");
		makerMap.put("53", "76");
		makerMap.put("52", "56");
		makerMap.put("51", "26");
		makerMap.put("50", "95");
		makerMap.put("49", "75");
		makerMap.put("48", "55");
		makerMap.put("47", "25");
		makerMap.put("46", "54");
		makerMap.put("45", "24");
		makerMap.put("44", "53");
		makerMap.put("43", "23");
		makerMap.put("42", "94");
		makerMap.put("41", "74");
		makerMap.put("40", "52");
		makerMap.put("39", "22");
		makerMap.put("38", "51");
		makerMap.put("37", "21");
		makerMap.put("36", "20");
		makerMap.put("35", "50");
		makerMap.put("34", "19");
		makerMap.put("33", "18");
		makerMap.put("32", "93");
		makerMap.put("31", "73");
		makerMap.put("30", "49");
		makerMap.put("29", "17");
		makerMap.put("28", "48");
		makerMap.put("27", "16");
		makerMap.put("26", "15");
		makerMap.put("25", "47");
		makerMap.put("24", "14");
		makerMap.put("23", "13");
		makerMap.put("22", "92");
		makerMap.put("21", "72");
		makerMap.put("20", "46");
		makerMap.put("19", "12");
		makerMap.put("18", "45");
		makerMap.put("17", "11");
		makerMap.put("16", "10");
		makerMap.put("15", "44");
		makerMap.put("14", "9");
		makerMap.put("13", "8");
		makerMap.put("12", "91");
		makerMap.put("11", "71");
		makerMap.put("10", "43");
		makerMap.put("9", "7");
		makerMap.put("8", "42");
		makerMap.put("7", "6");
		makerMap.put("6", "41");
		makerMap.put("5", "5");
		makerMap.put("4", "4");
		makerMap.put("3", "3");
		makerMap.put("2", "2");
		makerMap.put("1", "1");
	}

	public MakerTask()
	{
		this.username = "纺织任务";
	}

	@Override
	public void doTask()
	{
		try
		{
			Iterator<String> ite = UserCache.userQueue.keySet().iterator();
			while (ite.hasNext())
			{
				String username = ite.next();
				TaskManager.addTask(new Maker(username), GameUtil.randomInt(10));
			}
		} catch (Exception e)
		{
			LoggerUtil.error(e);
		}
	}

	/**
	 * 纺织任务
	 * 
	 * @author Administrator
	 * 
	 */
	class Maker extends TaskUnit
	{
		public Maker(String username)
		{
			this.username = username;
		}

		public void doTask()
		{
			try
			{
				UserData u = UserCache.get(username);
				if (u.getUserState() == UserState.LOGIN_GAME)
				{
					// 获取纺织信息
					HttpUtil.doGet(username, GameConstants.MAKE_QUERY_URL, new PreMakeAction(username));
				} else
				{
					// 添加登录任务先
					TaskManager.addTask(new LoginTask(username, this));
				}
			} catch (Exception e)
			{
				LoggerUtil.error(e);
			}
		}
	}

	/**
	 * 征战前查询
	 * 
	 * @author Administrator
	 * 
	 */
	class PreMakeAction extends AbstractActionAfterHttpRequest
	{
		public PreMakeAction(String username)
		{
			super();
			this.username = username;
			this.taskUnit = new Maker(username);
		}

		public void doCall(List<NameValuePair> headerList, XmlNodeData battleMultiInfo)
		{
			doMaker(battleMultiInfo);
		}

		/**
		 * 开始纺织
		 */
		private void doMaker(XmlNodeData battleMultiInfo)
		{
			String makenum = XmlDataUtil.getNodeData(battleMultiInfo, "results.makenum");
			String mcd = XmlDataUtil.getNodeData(battleMultiInfo, "results.mcd");
			String lv = XmlDataUtil.getNodeData(battleMultiInfo, "results.lv");
			String cdflag = XmlDataUtil.getNodeData(battleMultiInfo, "results.cdflag");
			if (GameUtil.convertInt(makenum) > 0)
			{
				if (cdflag.equals("0"))
				{
					// 还没有cd，那么开始搞呗
					HttpUtil.doGet(username, GameConstants.MAKE_WEAVER_ASS_URL, new MakerWeaverAction(username, lv));
				}
			}

		}
	}

	/**
	 * 发起攻击事后处理对象
	 * 
	 * @author Administrator
	 * 
	 */
	class MakerWeaverAction extends AbstractActionAfterHttpRequest
	{
		String lv = null;

		public MakerWeaverAction(String username, String lv)
		{
			super();
			this.lv = lv;
			this.username = username;
			this.taskUnit = new Maker(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData battleMultiAttackInfo)
		{
			// 纺织策略为，成功率小于30，然后求成功率与暴击率之和最高的
			List<XmlNodeData> xlist = XmlDataUtil.getNodeInfoList(battleMultiAttackInfo, "results.assplayer.weaverlevel");
			// 首先找能够纺织的级别最高的布
			int currLev = GameUtil.convertInt(lv);
			int maxLev = currLev;
			for (XmlNodeData xv : xlist)
			{
				int lev = GameUtil.convertInt(xv.getNodeValue());
				String weaversuc = XmlDataUtil.getNodeData(xv.getParent(), "weaversuc");
				String weavercri = XmlDataUtil.getNodeData(xv.getParent(), "weavercri");
				
				
				System.out.println(XmlDataUtil.getNodeData(xv.getParent(), "playername")+"\t"+lev+"\t"+weaversuc+"\t"+weavercri);
				if (maxLev < lev)
				{
					maxLev = lev;
				}
			}

			int canMakeLv = 0;
			for (Integer iv : bmakeList)
			{
				if (iv <= maxLev)
				{
					canMakeLv = iv;
					break;
				}
			}
			List<XmlNodeData> a1List = new ArrayList<XmlNodeData>();
			List<XmlNodeData> a2List = new ArrayList<XmlNodeData>();
			for (XmlNodeData xv : xlist)
			{
				int lev = GameUtil.convertInt(xv.getNodeValue());
				if (currLev >= canMakeLv)
				{
					a1List.add(xv);
				} else
				{
					if (lev >= canMakeLv)
					{
						a1List.add(xv);
					} else
					{
						a2List.add(xv);
					}
				}
			}
			
			
			Collections.sort(a1List, new MakerComparator());
			Collections.sort(a2List, new MakerComparator());

			
			// 如果a2为空，则只用在a1中取前2个即可，否则需要在a1，a2中各取一个
			String firstAssPId = "";
			String secondAssPId = "";
			if (a2List.isEmpty())
			{
				firstAssPId = XmlDataUtil.getNodeData(a1List.get(0).getParent(), "playerid");
				secondAssPId = XmlDataUtil.getNodeData(a1List.get(1).getParent(), "playerid");
				
				System.out.println(XmlDataUtil.getNodeData(a1List.get(0).getParent(), "playername")+"\t");
				System.out.println(XmlDataUtil.getNodeData(a1List.get(1).getParent(), "playername")+"\t");
			} else
			{
				firstAssPId = XmlDataUtil.getNodeData(a1List.get(0).getParent(), "playerid");
				secondAssPId = XmlDataUtil.getNodeData(a2List.get(0).getParent(), "playerid");
				System.out.println(XmlDataUtil.getNodeData(a1List.get(0).getParent(), "playername")+"\t");
				System.out.println(XmlDataUtil.getNodeData(a2List.get(0).getParent(), "playername")+"\t");
			}

			if (!firstAssPId.equals("") && !secondAssPId.equals(""))
			{
				// 开始纺织吧
				List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
				nvpList.add(new NameValuePair("firstAssPId", firstAssPId));
				nvpList.add(new NameValuePair("secondAssPId", secondAssPId));
				nvpList.add(new NameValuePair("makeId", makerMap.get(canMakeLv+"")));
				HttpUtil.doPost(username, GameConstants.MAKE_DO_WEAVER_ASS_URL, nvpList, new MakerAction(username));
			}
		}

	}

	/**
	 * 纺织操作回调事件
	 * 
	 * @author Administrator
	 * 
	 */
	class MakerAction extends AbstractActionAfterHttpRequest
	{
		public MakerAction(String username)
		{
			super();
			this.username = username;
			this.taskUnit = new Maker(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData battleMultiInfo)
		{
			String copper = XmlDataUtil.getNodeData(battleMultiInfo, "results.playerupdateinfo.copper");
			String profit = XmlDataUtil.getNodeData(battleMultiInfo, "results.playerupdateinfo.makeresult.makeresult.profit");
			LoggerUtil.info(UserCache.getGameName(username) + " 纺织获得银币：" + profit + "，当前银币数目为:" + copper);
			// 提交5秒钟后刷新任务,如果成功刷新等待开始攻击，否则重新征战任务
			TaskManager.addTask(new Maker(username), GameUtil.randomInt(10));
		}
	}

	class MakerComparator implements Comparator<XmlNodeData>
	{

		@Override
		public int compare(XmlNodeData o1, XmlNodeData o2)
		{
			String o1suc = XmlDataUtil.getNodeData(o1.getParent(), "weaversuc");
			String o1cri = XmlDataUtil.getNodeData(o1.getParent(), "weavercri");
			String o2suc = XmlDataUtil.getNodeData(o2.getParent(), "weaversuc");
			String o2cri = XmlDataUtil.getNodeData(o2.getParent(), "weavercri");
			int allo1 = GameUtil.convertInt(o1suc) + GameUtil.convertInt(o1cri);
			int allo2 = GameUtil.convertInt(o2suc) + GameUtil.convertInt(o2cri);
			if (allo1 > allo2)
			{
				return -1;
			} else if (allo1 == allo2)
			{
				if (GameUtil.convertInt(o1cri) >= GameUtil.convertInt(o2cri))
				{
					return -1;

				}else
				{
					return 1;
				}
			}
			return 1;
		}
	}
}
